package com.taotao.search.service.impl;
import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taotao.common.SearchItem;
import com.taotao.common.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;

import com.taotao.search.service.SearchItemService;
/**
 * 搜索系统的业务逻辑层接口实现类，将所有SearchItem导入到索引库中
 */
@Service
@Transactional
public class SearchItemServiceImpl implements SearchItemService {


	//注入solrServer
	@Autowired
	private SolrServer solrServer;

	//注入mapper
	@Autowired
	private SearchItemMapper itemMapper;

	/**
	 * 导入数据
	 * @throws IOException 
	 * @throws Exception 
	 */
	@Override
	public TaotaoResult importAllSearchItem() throws Exception {
			//		1、查询所有商品数据。
			List<SearchItem> searchItemList = itemMapper.getAllSearchItem();
			//		2、创建一个SolrServer对象。
			for (SearchItem searchItem : searchItemList) {
				//		3、为每个商品创建一个SolrInputDocument对象。
				SolrInputDocument document = new SolrInputDocument();
				//		4、为文档添加域
				document.addField("id", searchItem.getId());
				document.addField("item_title", searchItem.getTitle());
				document.addField("item_sell_point", searchItem.getSell_point());
				document.addField("item_price", searchItem.getPrice());
				document.addField("item_image", searchItem.getImages());
				document.addField("item_category_name", searchItem.getCategory_name());
				document.addField("item_desc", searchItem.getItem_desc());
		
				//5、向索引库中添加文档。
				solrServer.add(document);
			} 
			//提交
			solrServer.commit();
		//6、返回TaotaoResult。
		return TaotaoResult.ok();
	}
}
